package com.hoopchina.ui.home;

import java.util.ArrayList;
import com.hoopchina.news.R;
import com.hoopchina.util.UrlUtils;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
 * CategoryBar
 * 用于显示新闻类别的水平条
 * 
 * @author Neusoft
 */
public class CategoryBar extends LinearLayout {
    private static final String  TAG = "Category";
    private LinearLayout         mLinearLayout;
    private Context              mContext;
    private MyHorizontalScrollView mHScrollview;
    public  ArrayList<Button>    mButtons;
    
    private Button               mBtnArrowLeft;
    private Button               mBtnArrowRight;
    
    private ArrayList<String>    mNameList;
    public  int            mFocusedButtonIndex;
    public  int            mLastPosition=0;
    public  String         mLastPositionString=UrlUtils.HOME_ITEMS[0];
    public  String         mLastGoalPositionString=UrlUtils.GOAL_ITEMS[0];
    public CategoryBar( Context context ) {
        super( context );
        Log.d( TAG, "CategoryBar()" );
        init( context );
    }
    
    /*
     * 添加列表条目
     */
	public void addItemAll(ArrayList<String> aNameList) {
		Log.d(TAG, "addItemAll()");
		this.mNameList = aNameList;
		for (int i = 0; i < mNameList.size(); i++) {
			final Button btn = new Button(this.mContext);
			LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			if (i == 0) {
				param.leftMargin = dip2px(mContext, 5);
			} else {
				param.leftMargin = -dip2px(mContext, 8);
			}
			param.topMargin = dip2px(mContext, -1);
			btn.setText(mNameList.get(i));
			if (mNameList.get(i).length() == 2) {
				btn.setHeight(dip2px(mContext, 40));
				btn.setWidth(dip2px(mContext, 58));
			} else if (mNameList.get(i).length() == 3) {
				btn.setHeight(dip2px(mContext, 40));
				btn.setWidth(dip2px(mContext, 70));
			} else {
				btn.setHeight(dip2px(mContext, 40));
				btn.setWidth(dip2px(mContext, 110));
			}
			btn.setTextSize(15);
			btn.setTextColor(Color.parseColor("#b5b5b5"));
			btn.setPadding(0, 3, 0, 3);

			btn.setBackgroundResource(R.drawable.color_transparent);
			final int pos = i;
			btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					setFocusedButton(pos);
					if (subItemClickListener != null) {
						subItemClickListener.onSubItemClickListener(pos);
					}
				}
			});
			mLinearLayout.addView(btn, param);
			mButtons.add(btn);
		}
		setFocusedButton(0);
		mBtnArrowRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d(TAG, "arrow button onClick()");
				mHScrollview.fling(800);

			}
		});
		// mBtnArrowLeft.setOnClickListener( new OnClickListener() {
		//
		// @Override
		// public void onClick( View v ) {
		// Log.d( TAG, "arrow button onClick()" );
		// mHScrollview.fling( -800 );
		//
		// }
		// } );
	}
    
//    public void showLeftArrowButton(boolean isShow){
//    	if (isShow){
//    		// temp
//    		mBtnArrowLeft.setVisibility(Button.INVISIBLE);
//    	}
//    	else{
//    		mBtnArrowLeft.setVisibility(Button.INVISIBLE);
//    	}
//    }
    
	public void showRightArrowButton(boolean isShow) {
		if (isShow) {
			mBtnArrowRight.setVisibility(Button.VISIBLE);
		} else {
			mBtnArrowRight.setVisibility(Button.INVISIBLE);
		}
	}
    
	public void refreshItemAll(ArrayList<String> aNameList) {
		Log.d(TAG, "addItemAll()");
		// mLinearLayout.removeAllViews();
		this.mNameList = aNameList;
		for (int i = 0; i < mNameList.size(); i++) {
			mButtons.get(i).setText(mNameList.get(i));
			if (mButtons.get(i).length() == 2) {
				mButtons.get(i).setHeight(dip2px(mContext, 40));
				mButtons.get(i).setWidth(dip2px(mContext, 58));
			} else if (mNameList.get(i).length() == 3) {
				mButtons.get(i).setHeight(dip2px(mContext, 40));
				mButtons.get(i).setWidth(dip2px(mContext, 70));
			} else {
				mButtons.get(i).setHeight(dip2px(mContext, 40));
				mButtons.get(i).setWidth(dip2px(mContext, 110));
			}

		}
	}

	public CategoryBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public void showArrowButton() {
		showshowArrowButton(mHScrollview.getScrollX(), 0);
	}

	void showshowArrowButton(int x, int y) {
		// if (x <= 6) {
		// showLeftArrowButton(false);
		// } else if (x > 6) {
		// showLeftArrowButton(true);
		// }
		if (x >= 1090 - 20) {
			showRightArrowButton(false);
		} else if (x < 1090 - 20) {
			showRightArrowButton(true);
		}
	}
	
	/**

	* 根据手机的分辨率从 dip 的单位 转成为 px(像素)

	*/	
	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
}

	private void init(Context aContext) {
		Log.d(TAG, "init()");
		this.mContext = aContext;
		mButtons = new ArrayList<Button>();
		mButtons.clear();
		LayoutInflater inflater = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.categorynavibar, this, true);
		mHScrollview = (MyHorizontalScrollView) findViewById(R.id.category_scrollview);

		mBtnArrowLeft = (Button) findViewById(R.id.category_arrow_left);
		mBtnArrowRight = (Button) findViewById(R.id.category_arrow_right);

		mLinearLayout = (LinearLayout) findViewById(R.id.category_list);	
		
		mHScrollview.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return false;
			}
		});
		
		mHScrollview.setOnOverScrolledObserver(new IOnOverScrolledObserver() {

			@Override
			public void onScrollChanged(int l, int t, int oldl, int oldt) {
				if (l + mHScrollview.getWidth() >= mLinearLayout.getWidth()) {
					showRightArrowButton(false);
				} else {
					showRightArrowButton(true);
				}

			}

			@Override
			public void onOverScrolled(int scrollX, int scrollY) {

			}
		});
	}

	public void setFocusedButton(int aIndex) {
		if (aIndex < mButtons.size()) {
			mButtons.get(mFocusedButtonIndex).setBackgroundResource(
					R.drawable.color_transparent);
			mButtons.get(mFocusedButtonIndex).setTextColor(
					Color.parseColor("#b5b5b5"));
			mFocusedButtonIndex = aIndex;
			mButtons.get(aIndex).setBackgroundResource(
					R.drawable.image_categorybar_item_selected_background);
			mButtons.get(aIndex).setTextColor(Color.parseColor("#ffffff"));
			if (aIndex!=mButtons.size()-1) {
				mLastPosition=aIndex;
				mLastPositionString=mButtons.get(aIndex).getText().toString();
			}
			
		}
	}
    
	public ArrayList<Button> getButtonList() {
		return this.mButtons;
	}
    
	public Button getButton(int aIndex) {
		return mButtons.get(aIndex);
	}

    protected OnSubItemClickListener subItemClickListener;

	public void setOnSubItemClickListener(OnSubItemClickListener l) {
		this.subItemClickListener = l;
	}

	public interface OnSubItemClickListener {
		public void onSubItemClickListener(int postion);
	}
    
	
}